package aes.gui;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import aes.general.Teacher;
import aes.general.TeacherControl;

/**
 * The Class TeacherRequestPanel.This panel is handling the requests made by the teacher in order
 * to extend exam's time. Teacher is specifying the exam ID 
 * and the extended time he/she wants to add. 
 * This process is done by a request to the admin the will eventually
 * approve or decline the request.   
 */
public class TeacherRequestPanel extends JPanel {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel l1;
	private JLabel l2;
	private JLabel l3;
	private JLabel l4;
	private JButton b1;
	private JButton b2;
	private JButton b3;
	private JTextArea txt;
	private JComboBox cb;
	private JSeparator separator;
	private JTextField t1;
	
	private boolean flag = false;
	private ArrayList<String> active = new ArrayList<String>();
	private ArrayList<String> ExtendExam = new ArrayList<String>();
	
	/**
	 * Create the panel.
	 *
	 * @param contentPane the content pane
	 */
	public TeacherRequestPanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}
		
		/**
		 * Initialize.
		 */
		private void initialize() {
			setLayout(null);
			
			l1 = new JLabel("Make Exam Time Request");
			l1.setBounds(178, 11, 284, 29);
			l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
			add(l1);
			
			separator = new JSeparator();
			separator.setBounds(10, 43, 620, 2);
			add(separator);
			
			b1 = new JButton("Send");
			b1.setBounds(222, 392, 91, 23);
			add(b1);
			b1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					  if(txt.getText().trim().length() == 0)
				        	JOptionPane.showMessageDialog(null,
									"Error,No Request Is Filled!", "Error",
									JOptionPane.ERROR_MESSAGE);
					  else {  
					try{
				        new java.math.BigInteger(t1.getText());
				      
				        if(t1.getText().trim().length() < 3 && cb.getSelectedItem()!= "-------")
				        	
				        	ExtendExam.add(Teacher.getTeacherID());
				        	ExtendExam.add((String) cb.getSelectedItem());
				        	ExtendExam.add(t1.getText());
				        	ExtendExam.add(txt.getText());
				        	
				        	TeacherControl.setRequest(ExtendExam);
				        	try {
								Thread.sleep(600);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}

				        	cb.removeAllItems();
							cb.addItem("-------");
							flag = false;
							active.clear();
							ExtendExam.clear();
							CardLayout cl = (CardLayout)(jp.getLayout());
							cl.show(jp,"mainPanel");
							txt.setText(null);
							t1.setText(null);
				        
				        }catch(NumberFormatException ex) {
				        	JOptionPane.showMessageDialog(null,
									"Error,Extended Time Is Not A Number!", "Error",
									JOptionPane.ERROR_MESSAGE);
				        	ExtendExam.clear();
				        }
					  }
					
				}
			});

			
			b2 = new JButton("Cancel");
			b2.setBounds(332, 392, 91, 23);
			add(b2);
			b2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					cb.removeAllItems();
					cb.addItem("-------");
					flag = false;
					active.clear();
					ExtendExam.clear();
					txt.setText(null);
					t1.setText(null);
					CardLayout cl = (CardLayout)(jp.getLayout());
					cl.show(jp,"mainPanel");	
				}
			});
			
			cb = new JComboBox();
			cb.setBounds(222, 76, 83, 20);
			add(cb);
						
			l2 = new JLabel("Choose Exam To Extend:");
			l2.setBounds(28, 79, 155, 14);
			add(l2);
			
			l3 = new JLabel("Write Down The Request:");
			l3.setBounds(28, 123, 171, 14);
			add(l3);
			
			txt = new JTextArea();
			txt.setFont(new Font("Courier New", Font.BOLD, 12));
			txt.setBounds(28, 150, 557, 155);
			add(txt);
			
			l4 = new JLabel("Indicate The Desired Time Extend(In Minutes):");
			l4.setBounds(28, 331, 277, 14);
			add(l4);
			
			t1 = new JTextField();
			t1.setBounds(311, 328, 86, 20);
			add(t1);
			t1.setColumns(10);
			
			b3 = new JButton("Get Exams");
			b3.setBounds(332, 75, 107, 23);
			add(b3);
			cb.addItem("-------");
			b3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {	
					if(flag == false){
						TeacherControl.RequestExams(active);
						try {
							Thread.sleep(600);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						active = TeacherControl.getRunningExams(); 
						for (int i=0;i<active.size();i++)
							cb.addItem(active.get(i));
						flag = true;
					}
				}
			});

		}
}
